#
# Copyright (C) Since 2018 ~ 2025, Inc. All Rights Reserved
#
# @Time    : 2025/1/15-下午4:48
# @Author  : Timmy
# @Email   : tangping@cloudstream-tech.com
# @File    : web_window.py
# @Software: lottery
# @Function:
#
from PyQt5.QtCore import QUrl, Qt
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile
from PyQt5.QtWidgets import QMainWindow


class WebWindow(QMainWindow):
    def __init__(self, parent=None, x=0.5, y=1.0):
        super(WebWindow, self).__init__(parent)

        self.setGeometry(100, 100, 800, 600)
        self._home = f"http://localhost:8000/static/index.html"
        url = f"{self._home}?x={x}&y={y}"
        self.setWindowTitle("现场抽奖")
        self.browser = QWebEngineView()
        self.browser.setUrl(QUrl(url))
        self.setCentralWidget(self.browser)

    def set_url(self, url: str = ""):
        self._home = url
        self.browser.setUrl(QUrl(url))
        self.browser.reload()

    def set_scale(self, x=0.5, y=1.0):
        url = f"{self._home}?x={x}&y={y}"
        self.browser.setUrl(QUrl(url))
        self.browser.reload()

    def resizeEvent(self, event):
        self.browser.reload()

    def keyPressEvent(self, event):
        # print('----------0', event.key())
        if event.key() == Qt.Key_F11:
            if self.isFullScreen():
                self.showMaximized()
            else:
                self.showFullScreen()
        super().keyPressEvent(event)

    def clear_page_cache(self):
        profile = QWebEngineProfile.defaultProfile()
        profile.clearVisitedLinks([QUrl(self._home)])
        profile.clearHttpCache()
